<template>
	<div class="app">
		<view class="search"></view>

		<view class="list_view">
			<view class="tab_bar">
				<view :class="['tab_view', act == index ? 'active' : '']" v-for="(item, index) in tabview" @click="changeAct(index)">{{ item }}</view>
			</view>

			<view class="act_view">
				<view class="tab_bar_nav" :style="[{ top: topbox }]">
					<view class="nav_view"><selectview v-model="select1" :options="option1" @change="changeOpt"></selectview></view>
					<view class="nav_view" v-if="act != 1"><selectview v-model="select2" :options="option2" @change="changeOpt"></selectview></view>
					<view :class="['nav_view', allcate != '全部分类' ? 'color' : '']">
						<view @click="flag = true">{{ allcate }}</view>
					</view>
				</view>
				<view class="nav">
					<view class="nav-view" v-if="act == 0">
						<view class="hot_list" style="width: 680rpx;">
							<view v-for="(item, index) in list" @click="goDetail(item.id)">
								<view class="view_item">
									<view class="view_item_left">
										<image :src="item.mainImage"></image>
										<view class="times">{{ item.totalTime }}</view>
									</view>
									<view class="view_item_right" style="width: 375rpx;">
										<view class="item_right_top">{{ item.title }}</view>
										<view class="item-right-bottom">
											<view class="teacher">🗣️{{ item.nickName }}</view>
											<view class="count">
												<view class="price" v-if="item.isFree == 0">💰{{ item.priceOriginal }}</view>
												<view class="price" v-else>免费</view>
												<view class="people">▶️{{ item.studyTotal }}人在学</view>
											</view>
										</view> 
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="nav-view" v-if="act == 1">
						<view class="nav_view_cont" v-for="(item, index) in list">
							<view class="nav_view_view">
								<view class="nav_view_left">
									<span class="nav_view_title">{{ item.title }}</span>
									<span class="nav_view_conts">{{ item.summary }}</span>
								</view>
								<view class="nav_view_right" v-if="item.imageUrl"><image :src="item.imageUrl"></image></view>
							</view>
							<view style="display: flex;align-items: center;">
								<span style="font-size: 26rpx;color: #222;margin-right: 10rpx;">{{ item.nickName }}</span>
								<span style="font-size: 26rpx;color: #999;margin-right: 10rpx;">· {{ item.updateDate }}</span>
								<span style="font-size: 26rpx;color: #999;">· {{ item.thumhup }}赞</span>
							</view>
						</view>
					</view>
					<view class="nav-view" v-if="act == 2">
						<view class="a2" v-for="(item, index) in list">
							<view class="a3">{{ item.title }}</view>
							<view class="a4">
								<view class="a5">
									<span>{{ item.thumhup }}</span>
									回答&nbsp;·&nbsp;{{ item.viewCount }}浏览
								</view>
								<view class="a5">
									<span>{{ item.nickName }}</span>
									&nbsp;·&nbsp;{{ item.createDate }}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="model" v-show="flag">
				<view class="box" @click="flag = false" :style="[{ top: topboxs }]">
					<view class="left">
						<view class="list" v-for="(item, index) in option3" :key="index">
							<view class="li" :style="listNum == index ? 'border-left: 5rpx solid #007AFF;color:#0053fd' : ''" @click.stop="changeCate(index)">{{ item.name }}</view>
						</view>
					</view>
					<view class="right">
						<view class="list">
							<view
								class="cart"
								v-for="(item, index) in option3[listNum].labelList"
								:key="index"
								@click.stop="changeCateTwo(index, item.id)"
								:class="[cateindex == index ? 'color' : '']"
							>
								{{ item.name }}
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="tops" v-if="goTops" @click="goTop()"><image src="https://www.mescroll.com/img/mescroll-totop.png"></image></view>
		</view>
	</div>
</template>

<script>
import api from '../../api/api.js';
import selectview from '../../components/select-view';
export default {
	data() {
		return {
			tabview: ['课程', '文章', '问答'],
			act: 0,
			cont: {
				categoryId: '',
				content: null,
				current: 1,
				isFree: null,
				labelId: null,
				size: 10,
				sort: null
			},
			list: [],
			request: ['course', 'article', 'question'],
			select1: 1,
			select2: 1,
			option1: [
				{
					value: 1,
					label: '综合排序',
					sort: null
				},
				{
					value: 2,
					label: '最新排序',
					sort: 'new'
				},
				{
					value: 3,
					label: '热门排序',
					sort: 'hot'
				}
			],
			option2: [
				{
					value: 1,
					label: '全部课程',
					isFree: null
				},
				{
					value: 2,
					label: '付费课程',
					isFree: '0'
				},
				{
					value: 3,
					label: '免费课程',
					isFree: '0'
				}
			],
			option3: [
				{
					labelList: []
				}
			],
			goTops: false,
			listNum: 0,
			flag: false,
			allcate: '全部分类',
			topbox: '',
			topboxs: '',
			cateindex: 0
		};
	},
	methods: {
		// 修改顶部
		changeAct(index) {
			this.act = index;
			this.allcate = '全部分类';
			this.flag = false;
			this.select1 = 1;
			this.list = [];
			this.getList();
			this.getCategoryLists();
		},
		// 获取全部分类
		getCategoryLists(option) {
			api.getCategoryList().then(res => {
				this.option3 = res.data;
				this.option3.unshift({
					id: 0,
					labelList: [
						{
							id: 4,
							name: 'sgsdr'
						},
						{
							id: 5,
							name: 'sgsdr'
						},
						{
							id: 6,
							name: 'dsdfg'
						},
						{
							id: 7,
							name: 'sgsrghr'
						},
						{
							id: 8,
							name: 'asfgsdg'
						},
						{
							id: 9,
							name: 'fsdhrt'
						}
					],
					name: '全部分类'
				});
				this.option3.forEach(item => {
					item.labelList.unshift({
						name: '不限'
					});
				});
				if (option) {
					this.setopt(option);
				}
			});
		},
		// 修改一级列表
		changeCate(i) {
			if (i == 0) {
				this.allcate = '全部分类';
				this.flag = false;
				this.list = [];
				this.getList();
			}
			this.listNum = i;
		},
		// 修改二级列表
		changeCateTwo(i, id) {
			this.cateindex = i;
			if (i == 0) {
				this.allcate = this.option3[this.listNum].name;
				this.flag = false;
				this.cont.labelId = null;
			} else {
				this.allcate = this.option3[this.listNum].labelList[i].name;
				this.flag = false;
				this.cont.labelId = id;
			}
			this.list = [];
			this.getList();
		},
		// 获取列表
		getList() {
			api.getList(this.cont, this.request[this.act]).then(res => {
				if (res) {
					if (this.cont.current == 1) {
						this.list = res.data.records;
					} else {
						let arr = res.data.records;
						this.list.push(...arr);
					}
				}
			});
		},
		// 接受子组件传递的参数,发送请求
		changeOpt(e) {
			if (!e) {
				this.cont = {
					categoryId: '',
					content: null,
					current: 1,
					isFree: null,
					labelId: '',
					size: 10,
					sort: null
				};
			}
			if (e.sort) {
				this.cont.sort = e.sort;
			}
			if (e.isFree) {
				this.cont.isFree = Number(e.isFree);
			}
			this.getList();
		},
		// 跳转详情
		goDetail(id) {
			uni.navigateTo({
				url: '../detaile/detaile?id=' + id
			});
		},
		// 返回顶部
		goTop() {
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 300
			});
		},
		setopt(option) {
			
			if (option.id) {
				this.cont.categoryId = option.id;
					
				if (!option.act) {
					let index = this.option3.findIndex(item => {
						return item.id == option.id;
					});
					this.listNum = index;
					this.allcate = this.option3[index].name;
				}
			}
			if (option.content) {
				this.cont.content = option.content;
			}
			if (option.act && option.index) {
				console.log(option.id)
				this.listNum = option.act;
				this.cateindex = option.index;
				this.allcate = this.option3[option.act].labelList[option.index].name;
			}
		}
	},
	// 引入组件
	components: {
		selectview
	},
	// 获取数据
	onLoad(option) {
		console.log(option)
		this.getCategoryLists(option);
		this.getList();
	},
	// 上滑加载
	onReachBottom() {
		this.cont.current += 1;
		this.getList();
	},
	// 控制返回顶部
	onPageScroll(res) {
		res.scrollTop > 150 ? (this.goTops = true) : (this.goTops = false);
		res.scrollTop > 10 ? (this.topbox = '80rpx') : (this.topbox = '160rpx');
		res.scrollTop > 10 ? (this.topboxs = '160rpx') : (this.topboxs = '250rpx');
	}
};
</script>

<style scoped lang="scss">
.search {
	width: 100%;
	height: 80rpx;
	background: skyblue;
}

.list_view {
	.tab_bar {
		width: 750rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		z-index: 1000;
		background-color: white;

		.tab_view {
			width: 250rpx;
			height: 80rpx;
			font-size: 30rpx;
			line-height: 80rpx;
			position: relative;
			text-align: center;
		}
	}

	.act_view {
		background-color: #fff;

		.tab_bar_nav {
			width: 100%;
			position: fixed;
			left: 0;
			top: 160rpx;
			z-index: 100;
			font-size: 30rpx;
			line-height: 80rpx;
			display: flex;
			flex-direction: row;
			background-color: white;
			transform: 3s;

			.nav_view {
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
			}
		}

		.nav {
			padding: 100rpx 30rpx 0rpx;

			.hot_list {
				width: 660rpx;
				display: inline-block;
			}

			.view_item {
				width: 100%;
				margin: 20rpx 0rpx;
				display: flex;
				justify-content: space-between;
				border-bottom: 1rpx solid gray;
			}

			.oneview {
				display: inline-block;
				width: 310rpx;
				padding: 0 10rpx;
				margin-right: 20rpx;
				border-radius: 20rpx;
				box-shadow: 1rpx 1rpx 3rpx 2rpx rgba(0, 0, 0, 0.1);
				background-color: #fff;

				.oneview_div {
					width: 100%;
					padding: 20rpx 0;
					border-bottom: 1rpx solid #f1f1f1;

					.view_item_right {
						width: 290rpx;
						background-color: white;
					}
				}
			}

			.view_item_left {
				width: 290rpx;
				background-color: white;
				position: relative;

				image {
					width: 290rpx;
					height: 160rpx;
					border-radius: 10rpx;
				}

				.times {
					position: absolute;
					bottom: 12rpx;
					right: 5rpx;
					font-size: 22rpx;
					color: #fff;
					background: rgba(51, 51, 51, 0.4);
					border-radius: 20rpx;
					padding: 0 8rpx;
				}
			}

			.view_item_right {
				width: 350rpx;
				height: 160rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.item_right_top {
					width: 100%;
					height: 70rpx;
					line-height: 35rpx;
					font-size: 28rpx;
					font-weight: 700;
					overflow: hidden;
					text-overflow: ellipsis;
					-webkit-line-clamp: 2;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					white-space: normal;
				}

				.item-right-bottom {
					width: 100%;
					height: 75rpx;

					.teacher {
						font-size: 23rpx;
						color: #999;
						margin: 0;
					}

					.count {
						display: flex;
						align-items: center;

						.price {
							color: #fb6932;
							width: 160rpx;
							font-size: 28rpx;
							margin: 0;
						}

						.people {
							font-size: 23rpx;
							color: #222;
						}
					}
				}
			}
		}
	}
}

.tops {
	position: fixed;
	z-index: 9990;
	left: auto;
	right: 30rpx;
	bottom: 140rpx;

	image {
		width: 72rpx;
		height: 72rpx;
		border-radius: 50%;
	}
}

.tab_view:after {
	position: absolute;
	content: '';
	width: 0rpx;
	height: 0;
	border-bottom: 6rpx solid #007aff;
	border-radius: 20rpx;
	left: 50%;
	bottom: 6rpx;
	transform: translateX(-50%);
	transition: 0.3s;
}

.active {
	color: #007aff;
}

.active:after {
	position: absolute;
	content: '';
	width: 60rpx;
	height: 0;
	border-bottom: 6rpx solid #007aff;
	border-radius: 20rpx;
	left: 50%;
	bottom: 6rpx;
	transform: translateX(-50%);
	transition: 0.3s;
}

.model {
	position: fixed;
	left: 0;
	top: 170rpx;
	background-color: white;
}

.box {
	width: 100%;
	display: flex;
	position: fixed;
	top: 250rpx;
	left: 0;
	background-color: white;

	.left {
		width: 200rpx;
		background: #f8f9fb;
		overflow: scroll;
		max-height: 580rpx;
		padding-bottom: 50rpx;

		.list {
			height: 50rpx;
			padding: 50rpx 0;
			text-align: center;

			.li {
				height: 50rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}

	.right {
		width: 550rpx;
		height: 300rpx;

		.list {
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			justify-content: flex-start;

			.cart {
				width: 160rpx;
				height: 65rpx;
				margin-top: 25rpx;
				line-height: 65rpx;
				text-align: center;
				border-radius: 30rpx;
				margin-left: 20rpx;
				border: 2rpx solid #c0c0c0;
			}
		}
	}
}

.nav_view_cont {
	height: 236rpx;

	.nav_view_view {
		display: flex;
		justify-content: space-between;

		.nav_view_left {
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.nav_view_title {
				font-size: 35rpx;
				font-weight: 700;
			}

			.nav_view_conts {
				overflow: hidden;
				text-overflow: ellipsis;
				-webkit-line-clamp: 1;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				white-space: normal;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				white-space: normal;
				text-align: left;
			}
		}

		.nav_view_right {
			width: 220rpx;

			image {
				width: 220rpx;
				height: 140rpx;
			}
		}
	}
}

.color {
	color: #007aff;
}

.a2 {
	width: 100%;
	height: 140rpx;
	padding: 30rpx 0rpx;
	display: flex;
	flex-direction: column;
	justify-content: space-between;

	.a3 {
		width: 100%;
		height: 44rpx;

		font-size: 36rpx;
		font-weight: 800;
	}

	.a4 {
		display: flex;
		margin-top: 10rpx;
		color: #ccc;
		justify-content: space-between;
	}
}
</style>
